package com.terren.spider;

import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

import com.terren.spider.run.SpiderEntryGroupRunner;
import com.terren.spider.run.SpiderEntryRunner;
import com.terren.spider.run.SpiderKeywordRunner;
import com.terren.spider.run.SpiderKeywordTaskRunner;
import com.terren.spider.util.common.LogUtil;

public class Main {
	protected static Map<String, Object> map = new HashMap<String, Object>();

	public static void main(String[] args) throws Exception {
		if (args.length == 0) {
			usage(System.out);
			chooseMode(new Scanner(System.in));
			excute();
		} else {
			if (initParamMap(args, map)) {
				excute();
			}
		}
	}

	public static void excute() throws Exception {
		Integer mode = (Integer) map.get("mode");
		Integer entryid = (Integer) map.get("entryid");
		Integer groupid = (Integer) map.get("groupid");
		//Integer sourceid = (Integer) map.get("sourceid");
		//Integer limit = (Integer) map.get("limit");
		switch (mode) {
		case 1:// 搜索关键词爬取任务
			SpiderKeywordTaskRunner asSpider = new SpiderKeywordTaskRunner();
			asSpider.runTask();
			break;
		case 2:// 按组别进行爬取
			if (groupid != null) {
				SpiderEntryGroupRunner<?> groupRunner = new SpiderEntryGroupRunner<>(groupid);
				groupRunner.run();
			} else {
				System.out.println("There is no group id in the parameters,task is closed");
			}
			break;
		case 3:
			if (entryid != null) {
				SpiderEntryRunner<?> spider = new SpiderEntryRunner<>(entryid);
				spider.run();
			} else {
				System.out.println("There is no entry id in the parameters,task is closed");
			}
			break;
		case 4:// 抓取关键词任务
			SpiderKeywordRunner<?> runner = new SpiderKeywordRunner<>();
			runner.runTask();
			break;
		/*
		 * case 5://爬取搜索关键词链接、标题 SpiderArticleUrlSearchRunner urlSearchRunner = new
		 * SpiderArticleUrlSearchRunner(); urlSearchRunner.runTask(); break;
		 */
		// case 6://爬取搜索关键词网页内容
		// SpiderArticlePageSearchRunner pageSearchRunner = new
		// SpiderArticlePageSearchRunner();
		// pageSearchRunner.runTask();
		// break;

		default:
			throw new Exception("Unsupported arg:" + mode);
		}
	}

	public static void usage(PrintStream out) {
		String[] usage = { "爬虫程序入口: com.terren.spider.run.Main -m<mode> [-e<entryid>] [-g<groupid>] ",
				"\t-m<mode> - 参数-m 爬取内容和方式 [必填]" + "\n\t\t 1:SpiderKeywordTaskRunner 关键词临时任务爬取"
						+ "\n\t\t 2:SpiderEntryGroupRunner 分组爬取 " + "\n\t\t 3:SpiderEntryRunner 入口单元爬取"
						+ "\n\t\t 4:SpiderKeywordRunner 关键词爬取" 
				// + "\n\t\t 6:SpiderArticleSearchPageRunner 爬取搜索关键词网页内容"
				, "\t-e<entryid> - entry id 爬取单元入口编号[可选]", "\t-g<groupid> - group id 组别编号[可选]",
				"\n\t调用示例: /home/spider_worker/build/distributions/spider_worker/bin/spider_worker  " + "-m2 -g1 "
						+ "\n\t后台执行: (/home/spider_worker/build/distributions/spider_worker/bin/spider_worker -m1 &)"

		};

		for (String line : usage) {
			out.println(line);
		}
	}

	private static void chooseMode(Scanner scan) {
		map.clear();
		System.out.println("job mode:");
		System.out.println("1. [SpiderKeywordTaskRunner 关键词临时任务爬取]");
		System.out.println("2. [SpiderEntryGroupRunner 分组爬取 ]");
		System.out.println("3. [SpiderEntryRunner 入口单元爬取]");
		System.out.println("4. [SpiderKeywordRunner 关键词爬取]");
		// System.out.println("6. [SpiderArticleSearchPageRunner 爬取搜索关键词网页内容]");
		System.out.println("others. [exit]");
		int cmd = scan.nextInt();
		switch (cmd) {
		case 1:
			break;
		case 2:
			inputGroupId(scan);
			break;
		case 3:
			inputEntryId(scan);
			break;
		case 4:
			//inputSourceId(scan);
			break;
		case 5:
			break;
		case 6:
			break;
		default:
			exit();
		}
		map.put("mode", cmd);
	}

	private static void inputEntryId(Scanner scan) {
		System.out.println("entry id:");
		int cmd = scan.nextInt();
		map.put("entryid", cmd);
	}

	private static void inputGroupId(Scanner scan) {
		System.out.println("group id:");
		int cmd = scan.nextInt();
		map.put("groupid", cmd);
	}

	/*private static void inputSourceId(Scanner scan) {
		System.out.println("source id:");
		int cmd = scan.nextInt();
		map.put("sourceid", cmd);
	}*/
	/*
	 * private static void inputLimit(Scanner scan) { System.out.println("limit:");
	 * int cmd = scan.nextInt(); map.put("limit", cmd); }
	 */

	private static void exit() {
		System.exit(0); // do nothing
	}

	public static boolean initParamMap(String args[], Map<String, Object> map) {
		try {
			for (String arg : args) {
				if (arg.startsWith("-m")) {
					Integer mode = Integer.parseInt(arg.substring(2));
					map.put("mode", mode);
				} else if (arg.startsWith("-e")) {
					Integer id = Integer.parseInt(arg.substring(2));
					map.put("entryid", id);

				} else if (arg.startsWith("-g")) {
					Integer id = Integer.parseInt(arg.substring(2));
					map.put("groupid", id);
				} else if (arg.startsWith("-s")) {
					Integer id = Integer.parseInt(arg.substring(2));
					map.put("sourceid", id);
				}

				else if (arg.charAt(0) == '-') {
					usage(System.out);
				}
			}
		} catch (NumberFormatException e) {
			LogUtil.logger.error("parameter error", e);
			usage(System.out);
			return false;
		}
		if (map.size() == 0) {
			usage(System.out);
		}
		return map.size() > 0 ? true : false;
	}
}
